﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Reviewer.Entities.Services;
using Reviewer.Business;
using System.Globalization;
using Reviewer.Entities;

public partial class public_html_NewSiteTest_Releaseliste : System.Web.UI.Page
{
    public string CurrentLetterFilter
    {
        get
        {
            if (ViewState["CurrentLetterFilter"] == null)
            {
                return String.Empty;
            }

            return (String)ViewState["CurrentLetterFilter"];
        }
        set
        {
            ViewState["CurrentLetterFilter"] = value;
        }
    }

    public int CurrentFormatFilter
    {
        get
        {
            if (ViewState["CurrentFormatFilter"] == null)
            {
                return -1;
            }

            return (int)ViewState["CurrentFormatFilter"];
        }
        set
        {
            ViewState["CurrentFormatFilter"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindLatest();
        }
        BindLetters();
        BindFormats();
    }

    private void BindLetters()
    {
        this.uiRepLetters.DataSource = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,Æ,Ø,Å".Split(',');
        this.uiRepLetters.DataBind();
    }

    private void BindFormats()
    {
        using (IMetadataService<Format> service = ServiceFactory.GetMetadataService<Format>())
        {
            this.uiRepFormats.DataSource = service.GetAll().OrderBy(t => t.Name);
            this.uiRepFormats.DataBind();
        }
    }

    private void BindLatest()
    {
        uiLblHeadline.Text = "Seneste screenshots";

        using (IGameScreenshotService service = ServiceFactory.GameScreenshotService)
        {
            this.uiRepResults.DataSource = service.Search(null, null, null, 20);
            this.uiRepResults.DataBind();
        }
    }

    private void BindSearch()
    {
        using (IGameScreenshotService service = ServiceFactory.GameScreenshotService)
        {
            this.uiRepResults.DataSource = service.Search(String.IsNullOrEmpty(this.CurrentLetterFilter) ? String.Empty : this.CurrentLetterFilter + "%", this.CurrentFormatFilter, null, 20);
            this.uiRepResults.DataBind();
        }

        if (String.IsNullOrEmpty(this.CurrentLetterFilter) && this.CurrentFormatFilter < 1)
        {
            uiLblHeadline.Text = "Seneste screenshots";
        }
        else
        {
            if (!String.IsNullOrEmpty(this.CurrentLetterFilter) && this.CurrentFormatFilter < 1)
            {
                uiLblHeadline.Text = "Søgning på " + this.CurrentLetterFilter;
            }
            else if (!String.IsNullOrEmpty(this.CurrentLetterFilter) && this.CurrentFormatFilter > 0)
            {
                using (IMetadataService<Format> formatService = ServiceFactory.GetMetadataService<Format>())
                {
                    uiLblHeadline.Text = "Søgning på " + this.CurrentLetterFilter + " til " + formatService.GetAll().First(t => t.Id.Equals(this.CurrentFormatFilter)).Name;
                }
            }
            else
            {
                using (IMetadataService<Format> formatService = ServiceFactory.GetMetadataService<Format>())
                {
                    uiLblHeadline.Text = "Alle screenshots til " + formatService.GetAll().First(t => t.Id.Equals(this.CurrentFormatFilter)).Name;
                }
            }
        }
    }

    public void uiLbLetter_Click(object sender, EventArgs e)
    {
        string newLetterFilter = (sender as LinkButton).Text;
        if (this.CurrentLetterFilter.Equals(newLetterFilter))
        {
            CurrentFormatFilter = -1;
        }

        this.CurrentLetterFilter = newLetterFilter;
        BindSearch();
    }

    public void uiLbFormat_Click(object sender, EventArgs e)
    {
        int newFormatFilter = int.Parse((sender as LinkButton).CommandArgument);
        if (this.CurrentFormatFilter.Equals(newFormatFilter))
        {
            this.CurrentLetterFilter = String.Empty;
        }

        this.CurrentFormatFilter = newFormatFilter;
        BindSearch();
    }

    protected void uiRepLetters_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                LinkButton link = e.Item.FindControl("uiLbLetter") as LinkButton;
                if (link != null)
                {
                    if (link.ToolTip.Contains("{0}"))
                    {
                        link.ToolTip = String.Format(CultureInfo.InvariantCulture, link.ToolTip, e.Item.DataItem.ToString());
                    }
                }
                break;
        }
    }

    protected void uiRepFormats_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                LinkButton link = e.Item.FindControl("uiLbFormat") as LinkButton;
                if (link != null)
                {
                    if (link.ToolTip.Contains("{0}"))
                    {
                        link.ToolTip = String.Format(CultureInfo.InvariantCulture, link.ToolTip, (e.Item.DataItem as Format).Name);
                    }
                }
                break;
        }
    }
}
